package com.niit.mobileDevide.dataClean;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class MobileMapper extends Mapper<LongWritable, Text,Text, NullWritable> {

    //日志
    private final static Logger LOGGER = LoggerFactory.getLogger(MobileMapper.class);

    @Override
    protected void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
        String line = value.toString();
        String[] arr = line.split(",");

        int blankFields = 0;
        for(String field:arr){
            if(StringUtils.isBlank(field)){
                blankFields++;
            }
        }
        if(blankFields >=2){
            context.getCounter("DeleteCounter","deleted").increment(1L);
            LOGGER.warn("删除1行：{}",line);
            return;
        }

        context.write(new Text(StringUtils.join(arr,",")),NullWritable.get());



    }


}
